package org.kernely.security.dao;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.kernely.persistence.service.SessionService;
import org.kernely.security.persistence.Role;
import org.osgi.service.component.ComponentContext;

public class RoleDAO {
	/**
	 * SessionService service
	 */
	private static SessionService service;
	
	/**
	 * The group dao instance
	 */
	private static RoleDAO instance;

	/**
	 * Store the group dao instance 
	 * @return the group dao
	 */
	public static RoleDAO getInstance() {
		return instance;
	}
	
	/**
	 * Set the persistence session service
	 * @param sessionService the session service to set
	 */
	public final void setPersistenceService(SessionService sessionService) {
		service = sessionService;
	}
	
	/**
	 * Activate the component, called by SC runtime
	 * @param ctxt the activation context
	 */
	final protected void activate(ComponentContext ctxt) {
		instance = this;
	}
	
	/**
	 * Gets the session service.
	 * 
	 * @return the session service
	 */
	public static SessionService getService() {
		return service;
	}
	
	/**
	 * Sets the session service.
	 * 
	 * @param service
	 *            the session service to set
	 */
	public static void setService(SessionService service) {
		RoleDAO.service = service;
	}
	
	/**
	 * Returns all the roles stored
	 * @return a list of all roles stored
	 */
	public final Set<Role> getAllRoles(){
		Session session = service.getSessionFactory().getCurrentSession();
		Transaction t = session.beginTransaction();
		List<Role> list = (List<Role>) session.createCriteria(Role.class).list();
		Set<Role> roles = new HashSet<Role>(list);
		t.commit();
		if (session.isOpen()) {
			session.close();
		}
		return roles;
	}
	
	/**
	 * Returns the role which have this id
	 * @param id : the id of the needed role
	 * @return the corresponding role
	 */
	public final Role getRoleById(int id){
		Session session = service.getSessionFactory().getCurrentSession();
		Transaction t = session.beginTransaction();
		Criteria crit = session.createCriteria(Role.class);
		crit.add(Restrictions.like("id", id));
		Role g = (Role) crit.list().get(0);
		t.commit();
		if (session.isOpen()) {
			session.close();
		}
		return g;
	}
	
	/**
	 * Returns the role which have this name
	 * @param name : this name of the needed role
	 * @return : the corresponding role
	 */
	public final Role findRoleByName(String name){
		Session session = service.getSessionFactory().getCurrentSession();
		Transaction t = session.beginTransaction();
		Criteria crit = session.createCriteria(Role.class);
		crit.add(Restrictions.like("name", name));
		Role g = (Role) crit.list().get(0);
		t.commit();
		if (session.isOpen()) {
			session.close();
		}
		return g;
	}
	
	/**
	 * Persist the new role
	 * @param r : the new role
	 */
	public final void saveRole(Role r){
		Session session = service.getSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		session.save(r);
		t.commit();
		session.flush();
		if (session.isOpen()) {
			session.close();
		}
	}
	
	/**
	 * Update an existing role
	 * @param r : the role to update
	 */
	public final void updateRole(Role r){
		Session session = service.getSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		session.update(r);
		t.commit();
		session.flush();
		if (session.isOpen()) {
			session.close();
		}
	}
}
